package com.jy.datapipeline.export.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jy.datapipeline.common.constants.ExpConstants;
import com.jy.datapipeline.common.exception.GlobalException;
import com.jy.datapipeline.common.util.RandomNumsUtil;
import com.jy.datapipeline.export.common.constants.KeyConstant;
import com.jy.datapipeline.export.common.enums.impl.QueryTypeEnums;
import com.jy.datapipeline.export.common.enums.impl.TableTypeEnums;
import com.jy.datapipeline.export.dao.ExpRuleDetailDao;
import com.jy.datapipeline.export.dao.repo.ExpRuleDetailDaoRepo;
import com.jy.datapipeline.export.entity.ExpRuleDetailModel;
import com.jy.datapipeline.export.entity.ExpTaskModel;
import com.jy.datapipeline.export.service.ExpRuleDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.*;

@Service("expRuleDetailService")
@Transactional
public class ExpRuleDetailServiceImpl implements ExpRuleDetailService {
    @Autowired
    private ExpRuleDetailDao expRuleDetailDao;
    @Autowired
    private ExpRuleDetailDaoRepo expRuleDetailDaoRepo;
    @Override
    public ExpRuleDetailModel findByRuleIdAndSchemaNameAndRuleTbl(String ruleId, String schemaName,
        String ruleTbl) {
        return this.expRuleDetailDaoRepo.findByRuleIdAndSchemaNameAndRuleTbl(ruleId,schemaName,ruleTbl);
    }

    @Override
    public List<ExpRuleDetailModel> findByRuleId(String ruleId) {
        return this.expRuleDetailDaoRepo.findByRuleId(ruleId);
    }

    @Override
    public List<Map<String, Object>> findExpRuleDetailByList(String ruleId, String schemaName,
        String ruleTbl, int iDisplayStart, int iDisplayLength) {
        final String SCHEMA = "schema_name";
        final String TABLE = "rule_tbl";
        final String COLUMNS = "col_list";
        List<Map<String, Object>> expRuleDetailByList
                = this.expRuleDetailDao.findExpRuleDetailByList(ruleId, schemaName, ruleTbl, iDisplayStart, iDisplayLength);
        //转大写
        for (Map<String, Object> map : expRuleDetailByList) {
            for (String key : map.keySet()) {
                if (key.equals(SCHEMA) || key.equals(TABLE) || key.equals(COLUMNS)) {
                    map.put(key, ((String) map.get(key)).toUpperCase(Locale.ROOT));
                }
            }
        }
        return expRuleDetailByList;
    }

    @Override
    public int findExpRuleDetailByCount(String ruleId, String schemaName, String ruleTbl) {
        return this.expRuleDetailDao.findExpRuleDetailByCount(ruleId,schemaName,ruleTbl);
    }

    @Override
    public ExpRuleDetailModel findByRuleDetailId(String ruleDetailId) {
        return this.expRuleDetailDaoRepo.findByRuleDetailId(ruleDetailId);
    }

    @Override
    public boolean updateExpRuleDetail(ExpRuleDetailModel erdm) {
        return this.expRuleDetailDao.updateExpRuleDetail(erdm);
    }

    @Override
    public boolean save(ExpRuleDetailModel erdm) {
        this.expRuleDetailDaoRepo.save(erdm);
        return true;
    }

    @Override
    public boolean deleteById(int id) {
        this.expRuleDetailDaoRepo.deleteById(id);
        return true;
    }

    @Override
    public boolean deleteByRuleId(String ruleId) {
        return this.expRuleDetailDao.deleteByRuleId(ruleId);
    }

    @Override
    public void batchSave(List<ExpRuleDetailModel> expRuleDetailModelList) throws Exception {
        expRuleDetailDao.batchSave(expRuleDetailModelList);
    }

    @Override
    public void batchDeleteById(Set<String> ruleIds) throws Exception {
        expRuleDetailDao.batchDelete(ruleIds);
    }

    @Override
    public BigInteger getCount(BigInteger leftMin, BigInteger rightMax,
                               ExpRuleDetailModel expRuleDetailModel, String ruleTbl, DataSource dataSource) {
        return expRuleDetailDao.getCount(leftMin, rightMax, expRuleDetailModel, ruleTbl, dataSource);
    }

    @Override
    public Map<String, Object> getSplitKey(ExpRuleDetailModel expRuleDetailModel, String ruleTbl, Date dataDate, DataSource dataSource) throws SQLException {
        return expRuleDetailDao.getSplitKey(expRuleDetailModel, ruleTbl, dataDate, dataSource);
    }

    @Override
    public BigInteger getTotalCount(ExpRuleDetailModel expRuleDetailModel, String ruleTbl, DataSource dataSource, Date dataDate) {
        if (expRuleDetailModel.getRuleTYpe() != QueryTypeEnums.CUSTOM_QUERY.getCode() && expRuleDetailModel.getRuleTblType() == TableTypeEnums.DYNAMIC_TABLE.getCode()) {
            ruleTbl = RandomNumsUtil.getTable(dataDate, expRuleDetailModel.getRuleTblSuffix(), ruleTbl, expRuleDetailModel.getFunValue());
        }
        return expRuleDetailDao.getTotalCount(expRuleDetailModel, ruleTbl, dataSource, dataDate);
    }

    @Override
    public void updateExpRuleDetailIsAccurate(ExpTaskModel etm) {
        Object isAccurate = etm.getExtendColumns().get(KeyConstant.IS_ACCURATE_KEY);
        if (ObjectUtil.isNull(isAccurate)) {
            throw new GlobalException(ExpConstants.ADD_FAIL);
        }

        List<ExpRuleDetailModel> expRuleDetailModelList = this.findByRuleId(etm.getRuleId());
        for (ExpRuleDetailModel expRuleDetailModel : expRuleDetailModelList) {
            ExpRuleDetailModel ruleDetailModel = new ExpRuleDetailModel();
            ruleDetailModel.setRuleDetailId(expRuleDetailModel.getRuleDetailId());
            ruleDetailModel.setIsAccurate((int) isAccurate);
            expRuleDetailDao.updateExpRuleDetailIsAccurate(ruleDetailModel);
        }
    }
}